/*
 * FileUtils.java
 */

package behavior_analysis_control;

import java.io.File;
import java.util.Scanner;

/**
 * FileUtils is a class that contains static methods that assist
 * in processing files.
 * 
 * @author Sean Brown
 * @version 11/14/2012
 */
public class FileUtils {
	
	/**
	 * Validates a file to determine it exists and is in fact a file.
	 * 
	 * @param the_file_name The name of the file.
	 * @throws DialogErrorException Thrown when the file name is not valid.
	 */
	public static void validateFile(final String the_file_name) throws DialogErrorException{
		final File traces = new File(the_file_name);
		if (!traces.exists()) {
			throw new DialogErrorException("The file does not exist.");
		}
		if (!traces.isFile()) {
			throw new DialogErrorException("You have chosen an invalid file or folder.");
		} 
	}
	
	/**
	 * Returns the extension of the file name passed.
	 * 
	 * @param the_file_name The name of the file
	 * @return The extension of the file.
	 */
	public static String getExtension(final String the_file_name) throws DialogErrorException {
		int dot = the_file_name.lastIndexOf(".");
		if (dot == -1) {
			throw new DialogErrorException("This file does not have an extention.");
		}
	    final String extension = the_file_name.substring(dot + 1);
	    return extension;
	}
	
	/**
	 * Reads a line of text from the console.
	 * 
	 * @return A line of text from the console.
	 */
	public static String readFromConsole() {
		//start console scanner
		@SuppressWarnings("resource")
		Scanner console = new Scanner(System.in);
		return console.next();
	}
}
